from ConnectJDBCToMySql import *
import GDFSNcToJson
import LAPSNcToJson
import analysisNc_GDFS
import analysisNc_LAPS


# 获取未解析的nc文件
def getNcFile():
    ncList = []
    try:
        sql = "SELECT jrsj_filename,jrsj_location FROM tb_jrsj WHERE   jrsj_jxstorestatus = 0 and jrsj_fileformat = 'nc'"
        try:
            c1 = tosql3()
            c1[1].execute(sql)
            ncList = c1[1].fetchall()
        except Exception as e:
            print("getNcFile(2) ----- " + str(e))
        finally:
            c1[1].close()
            c1[0].close()
            return ncList
    except Exception as e:
        print("getNcFile(1) ----- " + str(e))


# 获取未解析nc文件在三个库里的jrsjid
def getJrsjId(fileName):
    try:
        global jrsjids
        sql = "SELECT jrsj_id FROM tb_jrsj WHERE jrsj_jxstorestatus = 0 and jrsj_filename = '%s'" % (fileName)
        try:
            c1 = tosql1()
            c2 = tosql2()
            c3 = tosql3()
            c1[1].execute(sql)
            jrsjid1 = c1[1].fetchone()
            c2[1].execute(sql)
            jrsjid2 = c2[1].fetchone()
            c3[1].execute(sql)
            jrsjid3 = c3[1].fetchone()

            jrsjids = dict()
            jrsjids['one'] = jrsjid1[0]
            jrsjids['two'] = jrsjid2[0]
            jrsjids['three'] = jrsjid3[0]

        except Exception as e:
            print("getJrsjId(2) ----- " + str(e))
        finally:
            c1[1].close()
            c1[0].close()
            c2[1].close()
            c2[0].close()
            c3[1].close()
            c3[0].close()
            return jrsjids
    except Exception as e:
        print("getJrsjId(1) ----- " + str(e))


def ncDataToMysql(filepath, jrsjid1, jrsjid2, jrsjid3):
    print("filepath" + str(filepath))
    if "OEFS_ECT" in filepath:
        layout_data = {
            'fileName': "Total_cloud_cover_cloud_base",
            'isnot': False,
            'needDataName': "cloud",
            'geoJsonTableName': "Cloud"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "OEFS_RHMI" in filepath:
        layout_data = {
            "get": "VAR0-1-121_FROM_38-0--1_height_above_ground_24_Hour_Minimum",
            'fileName': "height_above_ground_24_Hour_Minimum",
            'isnot': True,
            'needDataName': "humidity_min",
            'geoJsonTableName': "Humidity_min"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "OEFS_RHMX" in filepath:
        layout_data = {
            "get":"VAR0-1-122_FROM_38-0--1_height_above_ground_24_Hour_Maximum",
            'fileName': "height_above_ground_24_Hour_Maximum",
            'isnot': True,
            'needDataName': "humidity_max",
            'geoJsonTableName': "Humidity_max"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "OEFS_RRH" in filepath:
        layout_data = {
            'fileName': "Relative_humidity_height_above_ground",
            'isnot': True,
            'needDataName': "humidity",
            'geoJsonTableName': "Humidity"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "OEFS_TMAX" in filepath:
        layout_data = {
            'fileName': 'Maximum_temperature_height_above_ground_24_Hour_Maximum',
            'isnot': True,
            'needDataName': "temperature_max",
            'geoJsonTableName': "TempMax"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "OEFS_TMIN" in filepath:
        layout_data = {
            'fileName': "Minimum_temperature_height_above_ground_24_Hour_Minimum",
            'isnot': True,
            'needDataName': "temperature_min",
            'geoJsonTableName': "TempMin"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "OEFS_TMP" in filepath:
        layout_data = {
            'fileName': "Temperature_height_above_ground",
            'isnot': True,
            'needDataName': "temperature",
            'geoJsonTableName': "Temp"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "QPF_PPH" in filepath:
        layout_data = {
            'fileName': "Precipitation_type_surface_3_Hour_Accumulation",
            'isnot': False,
            'needDataName': "precipitation_type",
            'geoJsonTableName': "PreType"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "QPF_R03" in filepath:
        layout_data = {
            'fileName': "Total_precipitation_surface_3_Hour_Accumulation",
            'isnot': False,
            'needDataName': "precipitation_3_hour",
            'geoJsonTableName': "Pre3H"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "QPF_R06" in filepath:
        layout_data = {
            'fileName': "Total_precipitation_surface_6_Hour_Accumulation",
            'isnot': False,
            'needDataName': "precipitation_6_hour",
            'geoJsonTableName': "Pre6H"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "QPF_R12" in filepath:
        layout_data = {
            'fileName': "Total_precipitation_surface_12_Hour_Accumulation",
            'isnot': False,
            'needDataName': "precipitation_12_hour",
            'geoJsonTableName': "Pre12H"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "QPF_R24" in filepath:
        layout_data = {
            'fileName': "Total_precipitation_surface_24_Hour_Accumulation",
            'isnot': False,
            'needDataName': "precipitation_24_hour",
            'geoJsonTableName': "Pre24H"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "SFER_EDA10" in filepath:
        layout_data = {
            'fileName': "Wind_speed_gust_altitude_above_msl",
            'isnot': True,
            'needDataName': "wind_speed",
            'geoJsonTableName': "Wins"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "SHYS_VIS" in filepath:
        layout_data = {
            'fileName': "Visibility_surface",
            'isnot': False,
            'needDataName': "visibility_surface",
            'geoJsonTableName': "Visibility"
        }
        GDFSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3, layout_data)
    elif "LAPS3KM" in filepath:
        LAPSNcToJson.main(filepath, jrsjid1, jrsjid2, jrsjid3)


# 解析nc文件到mysql
def main():
    ncList = getNcFile()
    for item in ncList:
        fileName = item[0]
        filelocation = item[1]
        jrsjids = getJrsjId(fileName)
        jrsjid1 = jrsjids['one']
        jrsjid2 = jrsjids['two']
        jrsjid3 = jrsjids['three']
        # 解析nc数据
        ncDataToMysql(filelocation, jrsjid1, jrsjid2, jrsjid3)
        # 解析nc文件参数
        if "LAPS3KM" in fileName:
            # 解析3km nc文件参数
            print("解析3km nc文件参数")
            analysisNc_LAPS.main(filelocation, jrsjid3, fileName)
        else:
            # 解析5km nc文件参数
            print("解析5km nc文件参数")
            analysisNc_GDFS.main(filelocation, jrsjid3, fileName)


if __name__ == '__main__':
    main()
